package com.lzj.controller;

import com.lzj.entity.Department;
import com.lzj.entity.Employee;
import com.lzj.entity.Meeting;
import com.lzj.entity.MeetingRoom;
import com.lzj.service.*;
import freemarker.ext.beans.StringModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @autor:lzj
 * @date:2022/3/1
 */
@Controller
public class MeetingController {
    public static final int PAGE_SIZE=6;//一页显示多少条数据
    @Autowired
    MeetingRoomService meetingRoomService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    EmployeeService employeeService;
    @Autowired
    MeetingService meetingService;
    @Autowired
    MeetingParticipantsService meetingParticipantsService;

    @RequestMapping("/bookmeeting")
    public String bookmeeting(Model model){
        model.addAttribute("mrs",meetingRoomService.getAllMr());
        return "bookmeeting";
    }

    @RequestMapping("/allDeps")
    @ResponseBody
    public List<Department> getAllDeps(){
        return departmentService.getAllDeps();
    }

    @RequestMapping("/getEmpsByDepId/{depId}")
    @ResponseBody
    public List<Employee> getEmpsByDepId(@PathVariable int depId){
        return employeeService.getEmpsByDepId(depId);
    }

    @RequestMapping("/doAddMeeting")
    public String doAddMeeting(Meeting meeting, int[] mps, HttpSession session){
        Employee currentuser = (Employee) session.getAttribute("currentuser");
        meeting.setReservationistid(currentuser.getEmployeeid());//会议开始时间
        meeting.setReservationtime(new Date());
        for (int mp : mps) {
            System.out.println("员工id"+mp);
        }
        int i = meetingService.addMeeting(meeting);
        meetingService.addParticipants(meeting.getMeetingid(),mps);
        if(i==1){
            return "redirect:/searchmeetings";
        }else{
            return "forward:/bookmeeting";
        }
    }

    @RequestMapping("/searchmeetings")
    public String searchmeetings(Meeting meeting,String roomname, String employeename,@RequestParam(defaultValue = "1") int page, Model model){

        List<Integer> empsIdByDepName = employeeService.getEmpsIdByDepName(employeename);
        List<Integer> mrIdByName = meetingRoomService.getMrIdByName(roomname);
        Long total = meetingService.getTotal(meeting,empsIdByDepName,mrIdByName);
        if(page>(total%PAGE_SIZE==0?total/PAGE_SIZE:total/PAGE_SIZE+1)){
            page =(int)(total%PAGE_SIZE==0?total/PAGE_SIZE:total/PAGE_SIZE+1);
        }
        if(page<=0){
            page=1;
        }
        List<Meeting> allMeeting = meetingService.getAllMeeting(meeting,empsIdByDepName,mrIdByName, (page - 1) * PAGE_SIZE, PAGE_SIZE);
        for (Meeting meeting1 : allMeeting) {
            System.out.println("会议名称:"+meeting1.getMeetingname());
            System.out.println("人员名字："+meeting1.getEmployee().getEmployeename());
            System.out.println("会议室名字"+meeting1.getMeetingRoom().getRoomname());
        }

        model.addAttribute("mts",allMeeting);//查询到的对象
        model.addAttribute("total",total);//查询到的总数
        model.addAttribute("page",page);//第几页
        model.addAttribute("meeting",meeting);
        model.addAttribute("roomname",roomname);
        model.addAttribute("employeename",employeename);
        model.addAttribute("pagenum",total%PAGE_SIZE==0?total/PAGE_SIZE:total/PAGE_SIZE+1);//返回总页数
        return "searchmeetings";
    }

    @RequestMapping("/meetingdetails/{meetingid}")
    public String meetingdetails(@PathVariable int meetingid,Model model){
        List<Employee> empsByEmpId=null;
        Meeting meetingById = meetingService.getMeetingById(meetingid);
        List<Integer> empById = meetingParticipantsService.getEmpById(meetingid);//查询参会人员id
        if(empById!=null&&empById.size()>0){//防止参会人员为空造成sql报错
            System.out.println("执行了getEmpsByEmpId方法");
            empsByEmpId = employeeService.getEmpsByEmpId(empById);
        }
        model.addAttribute("meeting",meetingById);
        model.addAttribute("emps",empsByEmpId);
        return "meetingdetails";
    }

    @RequestMapping("/mybookings/{employeeid}")
    public String mybookings(@PathVariable int employeeid,Model model){
        List<Meeting> mybookings = meetingService.getMybookings(employeeid,"0");//0为未取消的会议
        model.addAttribute("meeting",mybookings);
        return "mybookings";
    }

    @RequestMapping("/mymeetingdetails/{meetingid}")
    public String mymeetingdetails(@PathVariable int meetingid,Model model){
        List<Employee> empsByEmpId=null;
        Meeting meetingById = meetingService.getMeetingById(meetingid);
        List<Integer> empById = meetingParticipantsService.getEmpById(meetingid);//查询参会人员id
        if(empById!=null&&empById.size()>0){//防止参会人员为空造成sql报错
            System.out.println("执行了getEmpsByEmpId方法");
            empsByEmpId = employeeService.getEmpsByEmpId(empById);
        }
        model.addAttribute("meeting",meetingById);
        model.addAttribute("emps",empsByEmpId);
        return "mymeetingdetails";
    }

    @RequestMapping("/cancelmeeting/{meetingid}")
    public String cancelmeeting(@PathVariable int meetingid,Model model){
        Meeting meetingById = meetingService.getMeetingById(meetingid);
        model.addAttribute("meeting",meetingById);
        return "cancelmeeting";
    }

    @RequestMapping("/updateMeeting")
    public String updateMeeting(Meeting meeting,HttpSession session){
        Employee currentuser = (Employee) session.getAttribute("currentuser");
        meetingService.updateMeetingStatusById(meeting,new Date(),"1");

        return "redirect:/mybookings/"+currentuser.getEmployeeid();
    }

    @RequestMapping("/mymeetings/{employeeid}")
    public String mymeetings(@PathVariable int employeeid,Model model){
        List<Meeting> meetingByMtIdAndSta=null;
        List<Integer> meetingByEmpId = meetingParticipantsService.getMeetingByEmpId(employeeid);
        if(meetingByEmpId!=null&&meetingByEmpId.size()>0){
            meetingByMtIdAndSta = meetingService.getMeetingByMtIdAndSta(meetingByEmpId, employeeid, "0");
        }
        model.addAttribute("meeting",meetingByMtIdAndSta);

        return "mymeetings";
    }
}
